package com.gzw.dms.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.google.gson.reflect.TypeToken;
import com.gzw.dms.MyApplication;

import com.gzw.dms.R;
import com.gzw.dms.adapter.QscxAdapter;
import com.gzw.dms.base.BaseFragment;
import com.gzw.dms.bean.ShippingOrderInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Gaozhiwei on 2015/12/25.
 */
public class QscxOrderFrmager extends BaseFragment {
    private ListView lv_dingdan;
    private List<ShippingOrderInfo> shippingOrderInfo;
    private Context mContext;
    private String state = "3";
    private SwipeRefreshLayout mSwipeLayout;
    private String rownum = "1";
    private String rows = "1000000";
    private String order_num = "";
    private String startTime ="";
    private String endTime ="";
    private String state2="";
    private String shiping_order_goid = "";
    private String custom_name = "";
    private String receipt_name = "";
    private int refresh = 0;
    private View progress;
    private ProgressDialog mProgressDialog;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected View setContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_sjqs, null);
    }

    @Override
    protected void initViews() {
        lv_dingdan = (ListView) findViewById(R.id.lv_dingdan);
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
        progress = findViewById(R.id.include_pb);

    }

    @Override
    protected void initEvents() {
        lv_dingdan.setOnItemClickListener(new mOnItemClick());

        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh = 1;
                order_num="";
                ShowOrderTask showOrderTask = new ShowOrderTask();
                showOrderTask.execute(state, rownum, rows, order_num,startTime,endTime,state2,shiping_order_goid,custom_name,receipt_name);

            }
        });
    }

    @Override
    protected void initData() {
        setHasOptionsMenu(true);

        mContext = getActivity();
        shippingOrderInfo = new ArrayList<>();
        refresh = 0;
        ShowOrderTask showOrderTask = new ShowOrderTask();
        showOrderTask.execute(state, rownum, rows, order_num,startTime,endTime,state2,shiping_order_goid,custom_name,receipt_name);
    }



    class mOnItemClick implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ShippingOrderInfo shippingOrder = (ShippingOrderInfo) parent.getItemAtPosition(position);
            Intent mIntetn = new Intent();
            Bundle bundle = new Bundle();
            mIntetn.setClass(mContext, ShippingOrderDetailActivity.class);
            bundle.putSerializable("shippingOrder", shippingOrder);
            mIntetn.putExtra("driver_id", "");
            bundle.putString("state", state);
            mIntetn.putExtra("fpstste","4");
            mIntetn.putExtras(bundle);
            startActivityForResult(mIntetn, 1);

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 1) {
            refresh = 0;
            ShowOrderTask showOrderTask = new ShowOrderTask();
            showOrderTask.execute(state, rownum, rows, order_num,startTime,endTime,state2,shiping_order_goid,custom_name,receipt_name);
        }

        else if (resultCode == 2) {
            refresh = 0;
            String order_num1 = (String) data.getExtras().get("order_num");
            String startTime1 = (String) data.getExtras().get("startTime");
            String endTime1 = (String) data.getExtras().get("endTime");
            String state21 = (String) data.getExtras().get("state2");
            String shiping_order_goid1 = (String) data.getExtras().get("shiping_order_goid");
            String custom_name1
                    = (String) data.getExtras().get("custom_name");
            String receipt_name1
                    = (String) data.getExtras().get("receipt_name");

            ShowOrderTask showOrderTask = new ShowOrderTask();
            showOrderTask.execute(state, rownum, rows, order_num1,startTime1,endTime1,state21,shiping_order_goid1,custom_name1,receipt_name1);
        }

    }

    public class ShowOrderTask extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {
            if (refresh == 0) {
//                progress.setVisibility(View.VISIBLE);
                mProgressDialog = ProgressDialog.show(mContext, null, "正在加载中...");
                mProgressDialog.setCancelable(true);
            } else if (refresh == 1) {

            }
        }

        protected String doInBackground(String... params) {
            Map<String, String> map = new HashMap<>();
            if(MyApplication.getUserInfo().getFlag().equals("1")) {
                map.put("driver_id", MyApplication.getUserInfo().getDriver_id());
                map.put("type","1");
            }else if (MyApplication.getUserInfo().getFlag().equals("2")){
                map.put("suppliers_id",MyApplication.getUserInfo().getSuppliers_id());
                map.put("driver_id",MyApplication.getUserInfo().getDriver_id());
                map.put("type","2");
            }
//            map.put("id",MyApplication.getUserInfo().getId());
            map.put("flag", params[0]);
            map.put("rownum", params[1]);
            map.put("rows", params[2]);
            map.put("order_num", params[3]);
            map.put("startTime", params[4]);
            map.put("endTime", params[5]);
            map.put("state2", params[6]);
            map.put("shiping_order_goid",params[7]);
            map.put("custom_name",params[8]);
            map.put("receipt_name",params[9]);
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("showOrder"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
            if (refresh == 0) {
//                progress.setVisibility(View.GONE);
                mProgressDialog.dismiss();

            } else if (refresh == 1) {
                mSwipeLayout.setRefreshing(false);

            }

            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {
                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else {
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    shippingOrderInfo = JsonUtil.getPersons(successMsg, new TypeToken<List<ShippingOrderInfo>>() {
                    }.getType());
                    QscxAdapter qscxAdapter = new QscxAdapter(mContext, shippingOrderInfo);
                    lv_dingdan.setAdapter(qscxAdapter);
                }
            }
        }
    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Inflate the menu_home; this adds items to the action bar if it is zpresent.
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_select, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_select:
                Intent mIntetn = new Intent();
                mIntetn.setClass(mContext, SelectActivity.class);
                startActivityForResult(mIntetn, 1);

                break;
        }
        return super.onOptionsItemSelected(item);
    }
}